//
//  JMStagemenVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/4/30.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMStagemenVController.h"
#import "JMStageTableViewCell.h"
#import "MJRefresh.h"
#import "JMStagemenReturn.h"
#import "JMStagemenDetailVController.h"

NSString *const JMStagemenCell = @"stagemenCell";

@interface JMStagemenVController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _pagNum;
    NSString * _token;
}
@property(nonatomic,weak)UIImageView *headImage;
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,weak)UIButton *backBtn;
@property(nonatomic,strong)NSMutableArray *stagemenArray;
@end

@implementation JMStagemenVController
- (NSMutableArray *)stagemenArray
{
    if (_stagemenArray == nil) {
        _stagemenArray = [NSMutableArray array];
    }
    return _stagemenArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createViews];
      _token = [UserEntity shardUserInfo].access_token;
    [self setupRefresh];
   
}
-(void)createViews
{
//    UIImageView *headImage = [ARC createImageViewWithFrame:CGRectMake(0, 0, 320, 180) ImageName:@"banner_Strategies"];
    UIImageView *headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 180)];
    headImage.image = [UIImage imageNamed:@"banner_Strategies"];
    headImage.frame = [ToolViewAndData MyAutoLayout:headImage];
    [self.view addSubview:headImage];
    UITableView *stageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,180, 320, 388) style:UITableViewStylePlain];
    stageTableView.frame = [ToolViewAndData MyAutoLayout:stageTableView];
    self.tableView = stageTableView;
    stageTableView.delegate = self;
    stageTableView.dataSource = self;
    [self.view addSubview:stageTableView];
    UIButton *backBtn = [ARC createButtonWithFrame:CGRectMake(0, 20, 50, 30) Target:self Sel:@selector(backToLast:)];
    backBtn.frame = [ToolViewAndData MyAutoLayout:backBtn];
    [backBtn setImage:[UIImage imageNamed:@"aut_ico_back"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    HIDDENNAV(YES);
}
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //warning 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableView.headerRefreshingText = @"正在帮你刷新中,不客气";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"正在帮你加载中,不客气";
    
}

//下拉刷新数据
- (void)headerRereshing
{
    _pagNum = 7;
    
    [self requestGetStrategyPeopleWithToken:_token page:1 pageSize:_pagNum IsHeaderR:YES];
}

-(void)footerRereshing
{
    _pagNum += 2;
    [self requestGetStrategyPeopleWithToken:_token page:1 pageSize:_pagNum IsHeaderR:NO];
    
    
}
-(void)requestGetStrategyPeopleWithToken:(NSString*)token page:(NSInteger)page pageSize:(NSInteger)pagesize IsHeaderR:(BOOL)isHeaderR
{
    NSString *url = [NSString stringWithFormat:@"%@%@&page=%ld&pageSize=%ld",JMURLGetStrategyPeopleList,token,(long)page,(long)pagesize];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        NSLog(@"dgfdsg%@",dict);
        JMStagemenReturn *stagemenList = [JMStagemenReturn objectWithKeyValues:dict];
        
        if ([stagemenList.State intValue]>= 0) {
            self.stagemenArray = [JMStagemenInfo objectArrayWithKeyValuesArray:stagemenList.data];
            
            
            NSMutableArray *aryM = [NSMutableArray array];
            for (JMStagemenInfo *stagemenInfo00 in aryM) {
                if (![self.stagemenArray containsObject:stagemenInfo00]) {
                    [aryM addObject:stagemenInfo00];
                }
            }
            [self.stagemenArray addObjectsFromArray:aryM];
            if (isHeaderR) {
                [self.tableView headerEndRefreshing];
            }else{
                [self.tableView footerEndRefreshing];
            }
            
            
            [self.tableView reloadData];
        }
        
    } FailBlock:^(NSString *string) {
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
}

-(void)backToLast:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stagemenArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    JMStageTableViewCell *cell =(JMStageTableViewCell*) [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[JMStageTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:JMStagemenCell];
        JMStagemenInfo *stagemenInfo = self.stagemenArray[indexPath.row];
        [JMData requestOrSetImgWithImageView:cell.personImage ProductImageUrl:stagemenInfo.HeadImg];
        cell.gradeNumber.text = stagemenInfo.Sort;
        if (stagemenInfo.NickName == NULL) {
            stagemenInfo.NickName = @"钱要多多";
        }
        cell.nameLabel.text = stagemenInfo.NickName;
        
        cell.levelLab.text = [NSString stringWithFormat:@"级别:"];
        cell.gradeNumber.textColor = [UIColor blackColor];
        cell.bindingLab.text = [NSString stringWithFormat:@"绑定:%@",stagemenInfo.BindNumber];
        cell.winProbaNum.text = stagemenInfo.PutWinRate;

        
        
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70*SCREEN_HEIGHT/568;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    JMStagemenDetailVController *stagemenDetailVC = [[JMStagemenDetailVController alloc]init];
    stagemenDetailVC.stagemenInfo = self.stagemenArray[indexPath.row];
    [self.navigationController pushViewController:stagemenDetailVC animated:YES];
}

@end